import { defineStore } from 'pinia'
import { getListApi, getSaveApi, getDelete, getModifyApi } from '../api/classify'

const orderMap: any = {
    ascend: 1,
    descend: -1
}

export const userclassifyStore = defineStore('classify', {
    state: () => {
        return {
            list: [],
            page: 1,
            pageSize: 3,
            total: '',
            by: undefined,
            order: 'ascend'

        }
    },
    getters: {

    },
    // 还可以访问其他getters

    actions: {
        async getLIst(name) {
            console.log(name)
            if (!this.order) {
                const { data, total } = await getListApi({
                    query: name,
                    page: this.page - 1,
                    pageSize: this.pageSize
                })
                // console.log(total)
                this.list = data
                // console.log(this.list)
                this.total = total

            } else {
                const { data, total } = await getListApi({
                    query: name,
                    page: this.page - 1,
                    pageSize: this.pageSize,
                    by: this.by,
                    order: orderMap[this.order]
                })
                // console.log(total)
                this.list = data
                // console.log(this.list)
                this.total = total
            }
        },
        // 新增
        async doAdd(data: Object) {
            const { _id } = data
            // console.log(data)
            if (_id) {
                await getModifyApi(data)
            } else {
                await getSaveApi(data)
            }
        },
        // 删除
        async doDelete(data) {
            const res = await getDelete({ _id: data })
        },
        // 修改
        // async doModify(data: object) {
        //     await getModifyApi(data)
        // }








    }
})